package com.example.musicplayer.ui;

import android.app.ActionBar;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.musicplayer.R;
import com.example.musicplayer.dao.MusicHandler;
import com.example.musicplayer.service.MusicServiceStub;
import com.example.musicplayer.service.MusicServiceStub.LocalBinder;

public class MainMusicPlayerActivity extends Activity {

	private UpDateUiReceiver receiver;
	private HeadphoneUnPlugIntent unplugReceiver;
	Context context = this;

	private TextView songTitleTxtField;
	private TextView songArtistTxtField;
	private ImageView albumThumbnail;
	private MusicHandler mHandler;
	MusicServiceStub musicService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mHandler = new MusicHandler(getContentResolver(), false, context);
		ActionBar actionBar = getActionBar();
		actionBar.hide();
		setContentView(R.layout.activity_main_music_player);
		//call to service layer to prepare the UI once music has been loaded		
		songTitleTxtField = (TextView)findViewById(R.id.song);		
		songArtistTxtField = (TextView)findViewById(R.id.artist);
		albumThumbnail = (ImageView)findViewById(R.id.thumbnail);
		// intent that handles pausing music when music is unplugged
		unplugReceiver = new HeadphoneUnPlugIntent();
		registerReceiver(unplugReceiver, new IntentFilter(
				android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY));
		receiver = new UpDateUiReceiver();// create the receiver
		registerReceiver(receiver, new IntentFilter("UpDateUI.action"));
	}

	protected void prepareMediaPlayer() {
		Intent musicServiceIntent = new Intent(this, MusicServiceStub.class);
		musicServiceIntent.setAction(MusicServiceStub.UPDATE_UI);
		startService(musicServiceIntent);
	}

	@Override
	protected void onStart() {
		super.onStart();
		Intent musicServiceIntent = new Intent(this, MusicServiceStub.class);
        bindService(musicServiceIntent, mConnection, Context.BIND_AUTO_CREATE);
		prepareMediaPlayer();
	}

	@Override
	protected void onPause() {
		unregisterReceiver(receiver);
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		registerReceiver(receiver, new IntentFilter("UpDateUI.action"));
	}

	private void updateMusicPlayerUI() {
		songTitleTxtField.setText(musicService.getCurrentlyPlayingSongTitle());
		songArtistTxtField.setText(musicService.getCurrentlyPlayingsongArtist());
		albumThumbnail.setImageURI(Uri.parse(mHandler.RetrieveAlbumArt(musicService.getCurrentlyPlayingalbumID())));
	}

	@Override
	protected void onStop() {
		super.onStop();
		unbindService(mConnection);
	}

	@Override
	protected void onDestroy() {
		unregisterReceiver(unplugReceiver);
		super.onDestroy();
	}

	public void playBtn(View v) {

		Intent musicServiceIntent = new Intent(this, MusicServiceStub.class);
		musicServiceIntent.setAction(MusicServiceStub.ACTION_PLAY);
		startService(musicServiceIntent);

	}

	public void forwardBtn(View v) {

		Intent musicServiceIntent = new Intent(this, MusicServiceStub.class);
		musicServiceIntent.setAction(MusicServiceStub.ACTION_FORWARD);
		startService(musicServiceIntent);
	}

	public void backBtn(View v) {

		Intent musicServiceIntent = new Intent(this, MusicServiceStub.class);
		musicServiceIntent.setAction(MusicServiceStub.ACTION_BACKWARD);
		startService(musicServiceIntent);

	}

	public void pauseBtn(View v) {

		Intent musicServiceIntent = new Intent(this, MusicServiceStub.class);
		musicServiceIntent.setAction(MusicServiceStub.ACTION_PAUSE);
		startService(musicServiceIntent);

	}

	public void repeatBtn(View v) {

		Intent musicServiceIntent = new Intent(this, MusicServiceStub.class);
		musicServiceIntent.setAction(MusicServiceStub.ACTION_REPEAT);
		startService(musicServiceIntent);

	}

	public void shuffleBtn(View v) {

		Intent musicServiceIntent = new Intent(this, MusicServiceStub.class);
		musicServiceIntent.setAction(MusicServiceStub.ACTION_SHUFFLE);
		startService(musicServiceIntent);

	}

	public void SongList(View v) {
		Intent SongListActivity = new Intent(this, SongListActivity.class);
		startActivity(SongListActivity);

	}

	public class UpDateUiReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			updateMusicPlayerUI();

		}

	}
	private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
            musicService = binder.getService();
            //mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
           // mBound = false;
        }

		
    };

}